package com.ruoyi.project.service.impl;

import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.domain.ConProcess;
import com.ruoyi.project.domain.OrderPreOrder;
import com.ruoyi.project.domain.ProProject;
import com.ruoyi.project.domain.ProProjectContract;
import com.ruoyi.project.mapper.ConProcessMapper;
import com.ruoyi.project.mapper.OrderPreOrderMapper;
import com.ruoyi.project.mapper.ProProjectContractMapper;
import com.ruoyi.project.mapper.ProProjectMapper;
import com.ruoyi.project.service.IConProcessService;

/**
 * 合同进度Service业务层处理
 * 
 * @author phil
 * @date 2020-06-02
 */
@Service
public class ConProcessServiceImpl implements IConProcessService{
	@Autowired
	private ConProcessMapper conProcessMapper;
	@Autowired
	private OrderPreOrderMapper orderPreOrderMapper;
	@Autowired
	private ProProjectContractMapper proProjectContractMapper;
	@Autowired
	private ProProjectMapper proProjectMapper;

	/**
	 * 查询合同进度
	 * 
	 * @param processId 合同进度ID
	 * @return 合同进度
	 */
	@Override
	public ConProcess selectConProcessById(Long processId){
		return conProcessMapper.selectConProcessById(processId);
	}

	/**
	 * 查询合同进度列表
	 * 
	 * @param conProcess 合同进度
	 * @return 合同进度
	 */
	@Override
	public List<ConProcess> selectConProcessList(ConProcess conProcess){
		return conProcessMapper.selectConProcessList(conProcess);
	}

	/**
	 * 新增合同进度
	 * 
	 * @param conProcess 合同进度
	 * @return 结果
	 */
	@Override
	public int insertConProcess(ConProcess conProcess){
		conProcess.setCreateTime(DateUtils.getNowDate());
		return conProcessMapper.insertConProcess(conProcess);
	}

	/**
	 * 修改合同进度
	 * 
	 * @param conProcess 合同进度
	 * @return 结果
	 */
	@Override
	public int updateConProcess(ConProcess conProcess){
		conProcess.setUpdateTime(DateUtils.getNowDate());
		return conProcessMapper.updateConProcess(conProcess);
	}

	/**
	 * 删除合同进度对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteConProcessByIds(String ids){
		return conProcessMapper.deleteConProcessByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除合同进度信息
	 * 
	 * @param processId 合同进度ID
	 * @return 结果
	 */
	@Override
	public int deleteConProcessById(Long processId){
		return conProcessMapper.deleteConProcessById(processId);
	}

	@SuppressWarnings("rawtypes")
	@Override
	@Transactional
	public int insertConProcessList(List<ConProcess> conProcessList, String userName, Long userId){
		int resultCount = 0;
		if(conProcessList != null && conProcessList.size() > 0){
			Set<Long> contractSet = new HashSet<Long>();
			for(ConProcess conProcess : conProcessList){
				resultCount += conProcessMapper.insertConProcess(conProcess);
				contractSet.add(conProcess.getConId());
				// 当合同进度时间已生效并且未回款金额不为0的数据，添加到派单池中。
				if(conProcess.getEffectiveTime() != null && !"".equals(conProcess.getEffectiveTime().toString()) && !"0".equals(conProcess.getReceivingAmount())){
					OrderPreOrder orderPreOrder = new OrderPreOrder();
					orderPreOrder.setConProcessId(conProcess.getProcessId());
					orderPreOrder.setStatus("0");
					List<OrderPreOrder> preOrderList = orderPreOrderMapper.selectOrderPreOrderList(orderPreOrder);
					if(preOrderList != null && preOrderList.size() > 0){
						continue;
					}
					orderPreOrder.setOrderName(conProcess.getContractName() + "-" + conProcess.getProcessName());
					orderPreOrder.setCollectibleRatio(conProcess.getCollectibleRatio());
					orderPreOrder.setIsDelete("0");
					orderPreOrder.setCreateBy(userName);
					orderPreOrder.setCreateTime(new Date());
					// 根据合同ID查询项目经理
					ProProjectContract proProjectContract = new ProProjectContract();
					proProjectContract.setConId(conProcess.getConId());
					List<ProProjectContract> list = proProjectContractMapper.selectProProjectContractList(proProjectContract);
					String userIds = "";
					String userNames = "";
					for(ProProjectContract proProjectContract2 : list){
						ProProject proProject = proProjectMapper.selectProProjectById(proProjectContract2.getProId());
						userIds += "," + proProject.getPmId();
						userNames += "," + proProject.getPmName();
					}
					if(!"".equals(userIds) && userIds.startsWith(",")){
						userIds = userIds.substring(1);
					}
					if(!"".equals(userNames) && userNames.startsWith(",")){
						userNames = userNames.substring(1);
					}
					orderPreOrder.setDispatchUserId(userIds);
					orderPreOrder.setDispatchUserName(userNames);
					orderPreOrderMapper.insertOrderPreOrder(orderPreOrder);
				}
			}

			// 重新计算当前合同进度
			Iterator it = contractSet.iterator();
			while(it.hasNext()){
				updateCurrentByConId((Long)it.next());
			}
		}
		return resultCount;
	}

	/**
	 * 更新最新的合同进度
	 * 
	 * @param conId
	 */
	public void updateCurrentByConId(Long conId){
		// 查询最近的进度记录
		List<ConProcess> conProcessList = conProcessMapper.selectListByEffective(conId);
		int size = 0;
		if(conProcessList != null && (size = conProcessList.size()) > 0){
			// 先把所有都设置成0
			if(size > 1){
				ConProcess otherProcess = new ConProcess();
				otherProcess.setConId(conId);
				otherProcess.setIsCurrent("0");
				conProcessMapper.updateCurrentByConId(otherProcess);
			}

			// 把当前设置成1
			ConProcess currentProcess = conProcessList.get(0);
			currentProcess.setIsCurrent("1");
			conProcessMapper.updateConProcess(currentProcess);
		}
	}

}
